<template>
  <div>
    <!-- 查询参数及按钮部分 -->
    <div class="filter-container">
      <el-dialog  v-dialogDrag  
        :close-on-click-modal="false"
        title="查询"
        :visible.sync="dialogQueryFormVisible"
        style="padding:0px"
        width="50%"
      >
        <el-form
          ref="dataForm"
          class="dialog-Backgroundcolor"
          :model="listQuery"
          label-position="right"
          label-width="100px"
          width="70%"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="工资类别：">
                <el-select
                  v-model="listQuery.meritPayCode"
                  class="filter-item"
                  clearable
                  placeholder="--请选择--"
                >
                  <el-option
                    label="行政人员"
                    value="0"
                  />
                  <el-option
                    label="事业人员"
                    value="1"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="职务：">
                <el-select
                  v-model="listQuery.posCode"
                  clearable
                  class="filter-item"
                  placeholder="--请选择--"
                >
                  <el-option
                    v-for="item in posgrLists"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="职级：">
                <el-select
                  v-model="listQuery.grCode"
                  clearable
                  class="filter-item"
                  placeholder="--请选择--"
                >
                  <el-option
                    v-for="item in grLists"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="人员姓名：">
                <el-input v-model="listQuery.personName" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="身份证号码：">
                <el-input v-model="listQuery.idenNo" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="人员类别：">
                <el-select
                  v-model="listQuery.state"
                  class="filter-item"
                  placeholder="--请选择--"
                >
                  <el-option
                    label="全部人员"
                    value=""
                  />
                  <el-option
                    label="在职人员"
                    value="3"
                  />
                  <el-option
                    label="离休人员"
                    value="1"
                  />
                </el-select>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="24">
              <el-button
                size="small"
                class="filter-item"
                style="float:right;margin-top:20px"
                type="primary"
                @click="handleQuery"
              >
                查询
              </el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>
      <el-row>

        <el-col :span="12">

          <el-select
            v-model="listQuery.state"
            class="filter-item"
            placeholder="--请选择--"
            @change="handleFilter"
          >
            <el-option
              label="全部人员"
              value=""
            />
            <el-option
              label="在职人员"
              value="3"
            />
            <el-option
              label="离休人员"
              value="1"
            />
          </el-select>

        </el-col>
      </el-row>
      <el-row>
        <el-col
          :span="24"
          class="numCol"
        >
          <span>单位名称：{{ unitName }} </span>
          <span> 在编人数：{{ registerNum }}人 其中：离休：{{ quitNum }}人</span>
        </el-col>

      </el-row>
    </div>
    <!-- 表格部分 -->
    <el-table
      ref="singalTable"
      v-loading="listLoading"
      :data="list"
      :stripe="true"
      fit
      :height="tableHeight"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed
      />
      <el-table-column
        type="index"
        align="center"
        width="50"
        label="序号"
        fixed
      />
      <el-table-column
        v-if="tabPane == 'auditAll'"
        label="变动状态"
        align="center"
        width="100"
        fixed
      >
        <template slot-scope="{row}">
          <span>{{ row.operateCode | changeFlag(row) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="tabPane != 'auditAll'"
        label="增减情况"
        align="center"
        width="100"
        fixed
      >
        <template slot-scope="{row}">
          <span>{{ row.operateCode | changeFlag(row) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="tabPane != 'auditAll'"
        label="变动原因"
        align="center"
        width="120"
        fixed
      >
        <template slot-scope="{row}">
          <span>{{ row.deleteType | parseReason(row) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="姓名"
        align="center"
        width="100"
        fixed
      >
        <template slot-scope="{row}">
          <el-link
            type="primary"
            @click="viewInfo(row)"
          >
            <span>{{ row.personName }}</span>
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="证件号码"
        align="center"
        width="200"
        fixed
      >
        <template slot-scope="{row}">
          <span>{{ row.idenNo }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="性别"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.sexCode | sexFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="民族"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.natCode | natFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="人员状态"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.state | personFlagFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="学历"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.schRecCode | schrecFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="职务"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.posCode | posgrFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="职级"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.grCode | grFilter(that) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="是否在编"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.isAuth | authFilter(that) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="工资级别"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.salaGrCode | salagrFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="出生日期"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.birthday }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="入职时间"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.enterAgencyDate }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="离退休时间"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.retireDate }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="参加工作时间"
        align="center"
        width="120"
      >
        <template slot-scope="{row}">
          <span>{{ row.workInitDate }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="备注"
        align="center"
        width="200"
      >
        <template slot-scope="{row}">
          <span>{{ row.remarks }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <Enclosure
      ref="enc"
      :middle-table-code="1"
    />
    <PersonSalaryInfo ref="personSalaryInfo" />
    <PersonChangeInfo ref="personChangeInfo" />

  </div>
</template>
<script>
import { getDepartmentList } from '@/api/unitInformationMaintenance/internalOrganizationMaintenance'
import {
  getPersonOperateList,
  getStateNum
} from '@/api/personInfoMaintenance/personChangeEdit'
import {
  rejectOperate,
  personApproved,
  rejectPersonApproved
} from '@/api/personInfoMaintenance/personChangeAudit'
import {
  getSex,
  getNat,
  getCitType,
  getSchrec,
  getPosgr,
  getAgencyPeride,
  getFundmode,
  getGr,
  getTecgr,
  getSalagr,
  getAgencyBank,
  getCountry, getSalaryState
} from '@/api/baseInfo'
import Enclosure from '@/components/Enclosure'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import tableEmpty from '@/components/table/empty'
import PersonSalaryInfo from '@/components/personSalaryInfo/personSalaryInfo'
import PersonChangeInfo from '@/components/personChangeInfo/personChangeInfo'
export default {
  name: 'PersonAudit',
  components: { Pagination, tableEmpty, Enclosure, PersonSalaryInfo, PersonChangeInfo },
  directives: { waves },
  filters: {
    operateCodeFilter(type, that) {
      if (type === 'add') {
        return '新增'
      } else if (type === 'edit') {
        return '修改'
      } else if (type === 'out') {
        return '调出'
      } else if (type === 'in') {
        return '调入'
      } else if (type === 'delete') {
        return '消减'
      }
    },
    changeFlag(type, row) {
      // const verify_state = row.verify_state
      if (type === 'add') {
        return '增员'
      } else if (type === 'edit') {
        return '修改'
      } else if (type === 'out') {
        return '减员'
      } else if (type === 'in') {
        return '增员'
      } else if (type === 'delete') {
        return '消减'
      }
    },
    parseReason(type, row) {
      if (row.operateCode === 'add') {
        return '新增'
      } else if (row.operateCode === 'edit') {
        return '修改人员信息'
      } else if (row.operateCode === 'out') {
        return '省内调出'
      } else if (row.operateCode === 'in') {
        return '省内调入'
      } else if (type === '0') {
        return '调出删除'
      } else if (type === '1') {
        return '办理退休'
      } else if (type === '2') {
        return '去世消减'
      } else if (type === '3') {
        return '工资停发'
      }
    },
    //号码脱敏显示
    hideMiddle(val) {
      if(val!== undefined&& val!== null){//避免列表中存在空数据报错
        return `${val.substring(0,4)}****${val.substring(val.length-4)}`
      }else{
        return val
      }
    },
    // 性别过滤器
    sexFilter(type, that) {
      const sexLists = that.sexLists
      for (const item in sexLists) {
        if (sexLists[item].code === type) {
          return sexLists[item].name
        }
      }
    },
    // 民族过滤器
    natFilter(type, that) {
      const natLists = that.natLists
      for (const item in natLists) {
        if (natLists[item].code === type) {
          return natLists[item].name
        }
      }
    },
    // 学历过滤器
    schrecFilter(type, that) {
      const schrecLists = that.schrecLists
      for (const item in schrecLists) {
        if (schrecLists[item].code === type) {
          return schrecLists[item].name
        }
      }
    },
    // 职务过滤器
    posgrFilter(type, that) {
      const posgrLists = that.posgrLists
      for (const item in posgrLists) {
        if (posgrLists[item].code === type) {
          return posgrLists[item].name
        }
      }
    },
    // 职级过滤器
    grFilter(type, that) {
      const grLists = that.grLists
      for (const item in grLists) {
        if (grLists[item].code === type) {
          return grLists[item].name
        }
      }
    },
    // 工资级别过滤器
    salagrFilter(type, that) {
      const salagrLists = that.salagrLists
      for (const item in salagrLists) {
        if (salagrLists[item].code === type) {
          return salagrLists[item].name
        }
      }
    },
    // 人员状态过滤器
    personFlagFilter(type, that) {
      if (type === '1') {
        return '离休'
      } else if (type === '3') {
        return '在职'
      }
    },
    // 是否在编过滤器
    authFilter(type, that) {
      if (type === 1) {
        return '是'
      } else if (type === 0) {
        return '否'
      }
    },
    deptNameFilter(value, that) {
      const deptNames = that.deptNames
      for (const item of deptNames) {
        if (value === item.code) {
          return item.name
        }
      }
    },
    // 工资项滤器
    salaryNameFilter(value, that) {
      const salaryNames = that.salaryNames
      for (const item of salaryNames) {
        if (item.value === value) {
          return item.key
        }
      }
    }
  },
  props: ['tabPane', 'treeData'],
  data() {
    return {
      tableHeight: null,
      salaryListLoading: false,
      salaryStatus: '工资信息',
      salaryTableVisible: false,
      salaryList: [],
      columnNames: [],
      salaryNames: [],
      deptNames: [],
      dialogQueryFormVisible: false,
      dialogStatus: '',
      that: this,
      unitName: '预算单位',
      registerNum: '0',
      quitNum: '0',
      list: [],
      total: 0,
      listLoading: false,
      openenClosure: true,
      salaryState: '',
      listQuery: {
        page: 1,
        limit: 200,
        meritPayCode: '',
        posCode: '',
        grCode: '',
        personName: '',
        idenNo: '',
        flag: 'auditAll',
        state: '',
        agencyId: ''
      },
      salaryListQuery: {
        page: 1,
        limit: 200,
        flag: 'all',
        agencyId: ''
      },
      formData: {
        id: '',
        state: '',
        operateCode: '',
        agencyCode: '',
        agencyName: '',
        meritPayCode: '',
        personName: '',
        sexCode: '',
        natCode: '',
        idenTypeCode: '',
        idenNo: '',
        birthday: '',
        schRecCode: '',
        personTypeCode: '',
        personAddress: '',
        isAuth: '',
        workInitDate: '',
        enterAgencyDate: '',
        posCode: '',
        grCode: '',
        tecGrCode: '',
        salaGrCode: '',
        salaCardNo: '',
        salaCardBank: '',
        govCredCardNo: '',
        govCredCardBank: '',
        startDate: '',
        nation: '',
        isUniSala: '',
        retireDate: '',
        workingYear: '',
        personIdeCode: '',
        deleteType: '',
        fundmode: '',
        isSpecialPost: '',
        outToAgency: '', // 去向单位code
        toAgencyName: '' // 去向单位名称
      },
      sexLists: [], // 性别
      natLists: [], // 民族
      citTypeLists: [], // 证件类型代码
      schrecLists: [], // 学历
      posgrLists: [], // 职务
      grLists: [], // 职级
      tecgrLists: [], // 技术等级
      salagrLists: [], // 工资级别
      agencyBankLists: [], // 银行
      countryLists: [], // 国籍
      personPerideLists: [], // 人员身份
      fundmodeLists: [], // 人员经费保障方式
      operatePersonLists: [] // 操作人员集合
    }
  },
  watch: {
    // 表格的数据数组
    list(val) {
      this.doLayout()
    },
    treeData(val) {
      if (val && val.isleaf === '1') {
        this.unitName = val.label.split('-')[1]
        this.listQuery.agencyId = val.id
        this.salaryListQuery.agencyId = val.id
        this.formData.agencyCode = val.code
        this.formData.agencyName = val.label.split('-')[1]
        this.handleTree(val)
      }
    }
  },
  created() {
    this.listQuery.flag = this.tabPane
    if (this.treeData && this.treeData.isleaf === '1') {
      this.unitName = this.treeData.label.split('-')[1]
      this.listQuery.agencyId = this.treeData.id
      this.salaryListQuery.agencyId = this.treeData.id
      this.formData.agencyCode = this.treeData.code
      this.formData.agencyName = this.treeData.label.split('-')[1]
      this.getSalaryState()
      this.initDict()
      this.getList()
    }
  },
  mounted() {
    this.getTbHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
  },
  methods: {
    // 获取table高度的方法
    getTbHeight() {
      this.tableHeight = window.innerHeight - 290
    },
    /* 重新渲染table组件 */
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singalTable.doLayout()
      })
    },
    // 附件方法
    appendixOpen() {
      this.selection = this.$refs.singalTable.selection
      const idList = []
      this.selection.forEach(item => {
        idList.push({ id: item.personId })
      })
      if (this.tabPane === '1') {
        this.openenClose = false
      }
      this.$refs.enc.appendixOpen(idList, this.openenClose, undefined, this.listQuery.agencyId)
    },
    getList() {
      this.listLoading = true
      getPersonOperateList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
      // 获取在职和离休人数
      this.getStateNum()
    },
    handleTree(data) {
      this.getSalaryState()
      this.initDict()
      this.getList()
    },
    handleFilter() {
      this.getList()
    },
    // 退回
    rejectOperate() {
      this.operatePersonLists = []
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择要退回的人员',
          type: 'warning',
          duration: 4000
        })
        this.$emit('rejectCallback')
        return
      }
      selection.forEach(item => {
        this.operatePersonLists.push(item)
      })
      // this.$confirm('确定退回吗?', '提示', {
      //   closeOnClickModal: false,
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      rejectOperate(this.operatePersonLists)
        .then(response => {
          this.$message({
            showClose: true,
            message: '退回成功',
            type: 'success',
            duration: 4000
          })
          this.$emit('rejectCallback')
          this.handleFilter()
        })
        .catch(() => {
          this.$emit('rejectCallback')
        })
        // })
        // .catch(() => {
        //   this.$emit('rejectCallback')
        // })
    },
    // 审核通过
    personApproved() {
      this.operatePersonLists = []
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择要审核的人员',
          type: 'warning',
          duration: 4000
        })
        this.$emit('approvedCallback')
        return
      }
      selection.forEach(item => {
        this.operatePersonLists.push(item)
      })
      // this.$confirm('确定审核吗?', '提示', {
      //   closeOnClickModal: false,
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      personApproved(this.operatePersonLists)
        .then(response => {
          this.$message({
            showClose: true,
            message: '审核成功',
            type: 'success',
            duration: 4000
          })
          this.$emit('approvedCallback')
          this.handleFilter()
        })
        .catch(() => {
          this.$emit('approvedCallback')
        })
        // })
        // .catch(() => {
        //   this.$emit('approvedCallback')
        // })
    },
    // 撤销审核
    rejectPersonApproved() {
      if (this.salaryState === 1) {
        this.$message({
          showClose: true,
          message: '单位工资已送审，不允许撤销审核！',
          type: 'warning',
          duration: 4000
        })
        this.$emit('rejectPersonApprovedCallback')
        return
      }
      const selection = this.$refs.singalTable.selection

      if (selection.length !== 1) {
        this.$message({
          showClose: true,
          message: '请选择一个要撤销审核的人员',
          type: 'warning',
          duration: 4000
        })
        this.$emit('rejectPersonApprovedCallback')
        return
      }
      rejectPersonApproved(selection[0]).then(res => {
        this.$message({
          showClose: true,
          message: '撤销审核成功',
          type: 'success',
          duration: 4000
        })
        this.$emit('rejectPersonApprovedCallback')
        this.handleFilter()
      }).catch(() => {
        this.$emit('rejectPersonApprovedCallback')
      })
    },

    // 获取在职和离职人数
    getStateNum() {
      getStateNum(this.listQuery).then(response => {
        if (response.data.registerNum !== '') {
          this.registerNum = response.data.registerNum + response.data.quitNum
        } else {
          this.registerNum = 0
        }
        if (response.data.quitNum !== '') {
          this.quitNum = response.data.quitNum
        } else {
          this.quitNum = 0
        }
      })
    },
    // 初始化字典
    initDict() {
      // 查询部门名称
      getDepartmentList({ agencyId: this.salaryListQuery.agencyId })
        .then(res => {
          this.deptNames = res.data
        })
        .catch(() => {
          this.deptNames = []
        })
      this.initBaseInfo()
    },
    // 查看详情
    viewInfo(currenRow) {
      this.$refs.personSalaryInfo.viewInfo(currenRow.personId, undefined, this.listQuery.agencyId)
    },
    initBaseInfo() {
      getSex().then(response => {
        this.sexLists = Object.assign({}, response.data)
      })
      getNat().then(response => {
        this.natLists = Object.assign({}, response.data)
      })
      getCitType().then(response => {
        this.citTypeLists = Object.assign({}, response.data)
      })
      getSchrec().then(response => {
        this.schrecLists = Object.assign({}, response.data)
      })
      getPosgr().then(response => {
        this.posgrLists = Object.assign({}, response.data)
      })
      getGr().then(response => {
        this.grLists = Object.assign({}, response.data)
      })
      getTecgr().then(response => {
        this.tecgrLists = Object.assign({}, response.data)
      })
      getSalagr().then(response => {
        this.salagrLists = Object.assign({}, response.data)
      })
      getAgencyBank().then(response => {
        this.agencyBankLists = Object.assign({}, response.data)
      })
      getCountry().then(response => {
        this.countryLists = Object.assign({}, response.data)
      })
      getAgencyPeride().then(response => {
        this.personPerideLists = Object.assign({}, response.data)
      })
      getFundmode().then(response => {
        this.fundmodeLists = Object.assign({}, response.data)
      })
    },
    // 查询
    query() {
      this.dialogQueryFormVisible = true
    },
    handleQuery() {
      this.listQuery.page = 1
      // this.listQuery.limit = 10
      this.getList()
      this.dialogQueryFormVisible = false
    },
    // 查询工资编报状态
    getSalaryState() {
      getSalaryState(this.listQuery).then(response => {
        this.salaryState = response.data.is_submit_in_agency
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "~@/styles/border.scss"; /*引入公共样式*/
.numCol {
  span {
    font-size: 16px;
  }
}
</style>
